﻿namespace CSharpPro._1103;
using System.Text.RegularExpressions;
public class UserServiceImpl
{
    private readonly List<User> _users = new List<User>();
    public void Run()
    {
        bool exit = false;
        while (!exit)
        {
            Console.WriteLine("\n=== 用户系统 ===");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择功能(1-3)：");

            if (!int.TryParse(Console.ReadLine(), out int choice) || choice < 1 || choice > 3)
            {
                Console.WriteLine("输入无效，请重新选择！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    exit = true;
                    Console.WriteLine("系统已退出！");
                    break;
            }
        }
    }
     public bool Register()
    {
        Console.WriteLine("\n=== 用户注册 ===");

     
        string username = GetValidUsername();
        if (string.IsNullOrEmpty(username))
            return false;


        string password = GetValidPassword();
        if (string.IsNullOrEmpty(password))
            return false;

   
        if (!ConfirmPassword(password))
            return false;

  
        _users.Add(new User(username, password));
        Console.WriteLine("注册成功！");
        return true;
    }


    public bool Login()
    {
        Console.WriteLine("\n=== 用户登录 ===");

        Console.Write("请输入用户名：");
        string username = Console.ReadLine()?.Trim() ?? string.Empty;

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不能为空！");
            return false;
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine() ?? string.Empty;

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不能为空！");
            return false;
        }

        
        foreach (var user in _users)
        {
            if (user.Username == username && user.Password == password)
            {
                Console.WriteLine("登录成功！");
                return true;
            }
        }

        Console.WriteLine("用户名或密码错误！");
        return false;
    }
    
    private string GetValidUsername()
    {
        Console.Write("请输入用户名（3-15位，含字母、数字、下划线）：");
        string username = Console.ReadLine()?.Trim() ?? string.Empty;

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不能为空！");
            return null;
        }

        if (username.Length < 3 || username.Length > 15)
        {
            Console.WriteLine("用户名长度必须为3-15位！");
            return null;
        }

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            Console.WriteLine("用户名只能包含字母、数字和下划线！");
            return null;
        }

    
        foreach (var user in _users)
        {
            if (user.Username == username)
            {
                Console.WriteLine("用户名已存在！");
                return null;
            }
        }

        return username;
    }

 
    private string GetValidPassword()
    {
        Console.Write("请输入密码（6-20位，含大小写字母和数字）：");
        string password = Console.ReadLine() ?? string.Empty;

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不能为空！");
            return null;
        }

        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度必须为6-20位！");
            return null;
        }

        if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
        {
            Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
            return null;
        }

        return password;
    }

   
    private bool ConfirmPassword(string password)
    {
        Console.Write("请确认密码：");
        string confirm = Console.ReadLine() ?? string.Empty;

        if (string.IsNullOrEmpty(confirm))
        {
            Console.WriteLine("确认密码不能为空！");
            return false;
        }

        if (password != confirm)
        {
            Console.WriteLine("两次输入的密码不一致！");
            return false;
        }

        return true;
    }

  
    public bool DeleteUser(string username)
    {
        if (string.IsNullOrEmpty(username))
            return false;

      
        
        for (int i = 0; i < _users.Count; i++)
        {
            if (_users[i].Username == username)
            {
                _users.RemoveAt(i);
                return true;
            }
        }
        return false;
    }
}


